/**
 * Copyright (C) 2012 The SkyTvOS Project
 * <p/>
 * Version     Date           Author
 * ─────────────────────────────────────
 * 2014年12月31日         cassie
 */

package com.coocaa.launcher.pattern.normal.allapp;

import android.app.Activity;
import android.app.Dialog;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.os.RemoteException;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.GridView;
import android.widget.TextView;

import com.coocaa.app.gamecenter.pages.home.GameMainActivity;
import com.coocaa.launcher.EasyLauncherActivity;
import com.coocaa.launcher.framework.launcherhost.LauncherHostView.LauncherHostPageView;
import com.coocaa.launcher.framework.launcherhost.view.AppItemView;
import com.coocaa.launcher.framework.manager.configdata.data.LauncherAppItemTableData.APP_ITEM_TYPE;
import com.coocaa.launcher.framework.manager.configdata.data.LauncherAppItemTableData.AppEntryData;
import com.coocaa.launcher.framework.manager.configdata.data.LauncherAppItemTableData.AppItemData;
import com.coocaa.launcher.framework.manager.configdata.data.LauncherAppItemTableData.AppShortCutData;
import com.coocaa.launcher.framework.manager.homekey.HomeKeymanager;
import com.coocaa.launcher.pattern.normal.NormalController;
import com.coocaa.launcher.pattern.normal.NormalController.BoundaryDetector;
import com.coocaa.launcher.pattern.normal.UIConfig;
import com.coocaa.launcher.pattern.normal.guide.GuideTips;
import com.coocaa.launcher.pattern.normal.guide.GuideView.GuideViewManager;
import com.coocaa.launcher.pattern.normal.guide.IGuideView.Tip;
import com.coocaa.launcher.pattern.normal.hotseat.FocusView;
import com.coocaa.launcher.pattern.normal.item.AppEntry;
import com.coocaa.launcher.pattern.normal.item.AppShortcut;
import com.coocaa.launcher.pattern.normal.util.ItemViewFactory;
import com.coocaa.launcher.util.PathConstants;
import com.coocaa.launcher.util.Utils;
import com.coocaa.x.framework.app.CoocaaApplication;
import com.coocaa.x.framework.utils.LiteURI;
import com.coocaa.x.service.XServiceManager;
import com.coocaa.x.service.litecontent.LiteContentServiceConnecter;
import com.coocaa.x.service.litecontent.data.apps.com_coocaa_app.AppData;
import com.coocaa.x.service.litecontent.data.apps.com_coocaa_app.RP_Installed_Apps;
import com.coocaa.x.service.litepm.data.AppInfo;
import com.coocaa.x.service.litepm.data.MActivityInfo;
import com.coocaa.x.uipackage.SkinManager;
import com.skyworth.logsdk.appstore.LogAPIForAppStore;
import com.tianci.appstore.AppStoreActivity;
import com.tianci.appstore.R;
import com.tianci.appstore.ui.widget.TipToast;
import com.tianci.appstore.utils.SkyConstants;

import java.util.ArrayList;
import java.util.List;

public class AllLocalAppView extends LauncherHostPageView {
    private Context mContext;
    private List<AppItemData> dataList = new ArrayList<AppItemData>();
    private TextView tipText;
    private GridView gridView;
    private FocusView focusImage;
    private int horizontalSpacing;
    private int verticalSpacing;
    private int firstItemLeft;
    private int hoverIndex = -1;

    private int selectedResId = R.drawable.hot_seat_focus_bg;
    private int gridViewWidth;
    private boolean doFocusAnim = true;

    private boolean menuShown = false;
    private Dialog menuDialog;
    private AllAppMenuView menu;
    private AppItemData menuData;
    private int clickMenuPos;

    private int focusImageFadeDuration = 300;

    public AllLocalAppView(Context context) {
        super("NormalAllLocalAppView", context);
        enableStatusBar(false);
        this.mContext = context;
        // TODO Auto-generated constructor stub
        this.setFocusable(false);
        this.setFocusableInTouchMode(false);
        this.setClickable(false);
        SkinManager.getInstance().setBackgroundResource(this, R.drawable.launcher_bg);

        submitLogInstalledApps();
        CoocaaApplication.post(new Runnable() {
            @Override
            public void run() {
                initDataList();
                createView();
            }
        });
    }

    private void initDataList() {
        dataList.clear();
        int filter = APP_ITEM_TYPE.APP_ENTRY_TYPE.flag;

        int entryFlag = APP_ITEM_TYPE.APP_ENTRY_TYPE.flag;
        if ((filter & entryFlag) == entryFlag) // 需要获取应用程序类型
        {
            // List<AppInfo> appList = AppManager.getInstance().getLocalApps().getInstalledApps();
            LiteURI uri = LiteURI.create("apps://com.coocaa.app/installed_apps");
            uri.addParams("filter", "launcherall");
            String json = null;
            try {
                json = LiteContentServiceConnecter.getService().call(uri.toString());
            } catch (RemoteException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            List<AppInfo> appList = new ArrayList<AppInfo>();
            if (json != null) {
                RP_Installed_Apps rp = RP_Installed_Apps
                        .parseJObject(json, RP_Installed_Apps.class);
                List<AppData> appdatas = rp.resource.resource_items;
                for (AppData appdata : appdatas)
                    appList.add(appdata.info);
            }
            if (appList != null && appList.size() > 0) {
                for (AppInfo info : appList) {
                    if (info.activityList != null && info.activityList.size() > 1) {
                        List<MActivityInfo> mList = info.activityList;
                        for (MActivityInfo aInfo : mList) {
                            if (!aInfo.className.equals(EasyLauncherActivity.class.getName())) {
                                AppEntryData data = new AppEntryData();
                                data.setTitle(aInfo.name);
                                data.setPackageName(info.pkgName);
                                data.setActivityName(aInfo.className);
                                data.setSystem(info.isSystemApp);
                                if (aInfo.className.equals(AppStoreActivity.class.getName()))
                                    dataList.add(0, data);
                                else if (aInfo.className.equals(GameMainActivity.class.getName())) {
                                    if (dataList.size() == 0) {
                                        dataList.add(0, data);
                                    } else {
                                        dataList.add(1, data);
                                    }
                                } else
                                    dataList.add(data);
                            }
                        }
                    } else {
                        AppEntryData data = new AppEntryData();
                        data.setTitle(info.appName);
                        data.setPackageName(info.pkgName);
                        data.setActivityName(info.mainActivity);
                        data.setSystem(info.isSystemApp);
                        if (!info.pkgName.equals("com.tianci.appstore"))
                            dataList.add(data);
                    }
                }
            }
        }
    }

    // 创建全部应用view时，添加日志搜集：单台设备安装应用总数
    private void submitLogInstalledApps() {
        CoocaaApplication.post(new Runnable() {
            @Override
            public void run() {
                List<String> pkgNames = new ArrayList<String>();
                for (AppItemData data : dataList)
                    pkgNames.add(data.getPackageName());
                LogAPIForAppStore.appThirdPartyList(pkgNames);
            }
        });
    }

    private void createView() {
        if (dataList.size() == 0) {
            tipText = new TextView(mContext);
            tipText.setTextColor(Color.WHITE);
            tipText.setText("木有找到对应的Activity");
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    addView(tipText);
                }
            });
        } else {
            focusImage = new FocusView(mContext);
            SkinManager.getInstance().setBackgroundResource(focusImage, selectedResId);
            initGridView();

            int viewWidth = UIConfig.hotSeatItemWidth + 2 * UIConfig.focusBorderWidth;
            int viewHeight = UIConfig.appItemHeight + 2 * UIConfig.focusBorderWidth;
            final FrameLayout.LayoutParams initFocusParams = new FrameLayout.LayoutParams(viewWidth,
                    viewHeight);
            initFocusParams.leftMargin = (Utils.getScreenWidth(mContext) - gridViewWidth) / 2
                    - UIConfig.focusBorderWidth;
            initFocusParams.topMargin = verticalSpacing - UIConfig.focusBorderWidth;
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    gridView.setSelection(0);
                    gridView.scrollTo(0, 0);
                    addView(focusImage, initFocusParams);
                }
            });
        }
    }

    private void initGridView() {
        // gridView = new GridView(mContext);
        gridView = (GridView) ((Activity) mContext).getLayoutInflater().inflate(
                R.layout.gridview_customize, null);// new GridView(this);
        gridView.setSelector(android.R.color.transparent);
        gridView.setNumColumns(UIConfig.PickAppColumnCount);
        gridView.setSmoothScrollbarEnabled(true);
        gridView.setAdapter(new PickAdapter(mContext, dataList));
        horizontalSpacing = SkyConstants.Div(71);
        verticalSpacing = SkyConstants.Div(93);
        gridViewWidth = UIConfig.PickAppColumnCount * UIConfig.hotSeatItemWidth
                + (UIConfig.PickAppColumnCount - 1) * horizontalSpacing;
        firstItemLeft = (Utils.getScreenWidth(mContext) - gridViewWidth) / 2;
        gridView.setVerticalSpacing(verticalSpacing);
        gridView.setColumnWidth(horizontalSpacing + UIConfig.hotSeatItemWidth);
        gridView.setStretchMode(GridView.NO_STRETCH);
        gridView.setPadding(firstItemLeft, verticalSpacing, 0, verticalSpacing);
        // 不知道为什么setOnItemClickListener完全没有响应。。。
        // 找到原因了，adapter中的itemView不可获取焦点，否则会截掉“确定”键，则不会执行itemClick
        gridView.setOnItemClickListener(itemClickListener);
        gridView.setOnItemSelectedListener(gridItemSelectListener);
        gridView.setOnKeyListener(gridKeyListener);
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                addView(gridView, new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT,
                        LayoutParams.MATCH_PARENT));
            }
        });
    }

    @Override
    public void onThemeChange() {
        // TODO Auto-generated method stub
        SkinManager.getInstance().setBackgroundResource(this, R.drawable.launcher_bg);
        SkinManager.getInstance().setBackgroundColor(focusImage, selectedResId);
    }

    private OnItemClickListener itemClickListener = new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            // TODO Auto-generated method stub
            clickItem(position);
        }
    };

    private OnClickListener gridClickListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            if (v.getId() >= 0 && v.getId() < dataList.size())
                clickItem(v.getId());
        }
    };

    private void clickItem(int pos) {
        // TODO Auto-generated method stub
        AppItemData info = dataList.get(pos);

        String packageName = ((AppEntryData) info).getPackageName();
        String activityName = ((AppEntryData) info).getActivityName();
        try {
            XServiceManager.getLitePMService().startAppByComponentName(mContext.getClass().getName(), false,
                    new ComponentName(packageName, activityName));
        } catch (Exception e) {
            e.printStackTrace();
        }
        submitLogLaunchApp(info);

        Tip readTip = GuideViewManager.getTipByKeyCode(KeyEvent.KEYCODE_ENTER,
                GuideTips.ALLAPPS_TIPS);
        if (readTip != null)
            GuideViewManager.getInstance().readTip(readTip);
    }

    private void submitLogLaunchApp(AppItemData data) {
        int appId = -1;
        try {
            appId = Integer.parseInt(data.getId());
        } catch (Exception e) {
        }
        String appName = data.getTitle();
        String pkgName = data.getPackageName();
        LogAPIForAppStore.appStartPath(PathConstants.LaunchAppPath.ALL_APPS.toString(), appId,
                pkgName, appName);

        // 提交启动应用圈的路径
        if (pkgName.equals(mContext.getPackageName()))
            LogAPIForAppStore.appStoreStartPath(PathConstants.LaunchAppStorePath.ALL_APPS
                    .toString());
    }

    private OnItemSelectedListener gridItemSelectListener = new OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            // TODO Auto-generated method stub
            int left = view.getLeft() - UIConfig.focusBorderWidth + gridView.getLeft();
            int top = view.getTop() - UIConfig.focusBorderWidth + gridView.getTop();
            int viewWidth = view.getWidth() + 2 * UIConfig.focusBorderWidth;
            int viewHeight = view.getHeight() + 2 * UIConfig.focusBorderWidth;
            if (!doFocusAnim) {
                focusImage.setFocusPos(left, top, viewWidth, viewHeight);
                doFocusAnim = true;
            } else
                focusImage.changeFocusPos(left, top, viewWidth, viewHeight);
            if (focusImage.getAlpha() != 1) {
                // if(focusImage.animate() != null)
                // focusImage.animate().cancel();
                // focusImage.setAlpha(1);
                focusImage.animate().alpha(1).setDuration(50);
            }
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {
            // TODO Auto-generated method stub
            focusImage.setAlpha(0);
        }
    };

    private OnKeyListener gridKeyListener = new OnKeyListener() {
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            // TODO Auto-generated method stub
            if (event.getAction() == KeyEvent.ACTION_DOWN) {
                if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT || keyCode == KeyEvent.KEYCODE_DPAD_RIGHT
                        || keyCode == KeyEvent.KEYCODE_DPAD_UP
                        || keyCode == KeyEvent.KEYCODE_DPAD_DOWN) {
                    if (hoverIndex != -1) {
                        if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT) {
                            if (hoverIndex > 0)
                                gridView.setSelection(hoverIndex - 1);
                            else
                                gridView.setSelection(hoverIndex);
                        }
                        if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT) {
                            if (hoverIndex < dataList.size() - 1)
                                gridView.setSelection(hoverIndex + 1);
                            else
                                gridView.setSelection(hoverIndex);
                        }
                        if (keyCode == KeyEvent.KEYCODE_DPAD_UP) {
                            if (hoverIndex >= gridView.getNumColumns())
                                gridView.setSelection(hoverIndex - gridView.getNumColumns());
                            else
                                gridView.setSelection(hoverIndex);
                        }
                        if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN) {
                            if (hoverIndex < dataList.size() - gridView.getNumColumns())
                                gridView.setSelection(hoverIndex + gridView.getNumColumns());
                            else
                                gridView.setSelection(hoverIndex);
                        }
                        hoverIndex = -1;
                        return true;
                    }
                }
                if (keyCode == KeyEvent.KEYCODE_DPAD_UP) {
                    if (gridView.getSelectedItemPosition() / gridView.getNumColumns() == 0) {
                        if (keyCode == KeyEvent.KEYCODE_DPAD_UP) {
                            if (boundaryDetector != null) {
                                // focusImage.animate().alpha(0).setDuration(focusImageFadeDuration);
                                boundaryDetector.switchToHotSeat();
                                GuideViewManager.getInstance().hideMe();
                                return true;
                            }
                        }
                    }
                }
            }
            return false;
        }
    };

    private OnHoverListener hoverListener = new OnHoverListener() {
        @Override
        public boolean onHover(View v, MotionEvent event) {
            // TODO Auto-generated method stub
            if (event.getAction() == MotionEvent.ACTION_HOVER_MOVE) {
                int left = v.getLeft() - UIConfig.focusBorderWidth + gridView.getLeft();
                int top = v.getTop() - UIConfig.focusBorderWidth + gridView.getTop();
                int viewWidth = v.getWidth() + 2 * UIConfig.focusBorderWidth;
                int viewHeight = v.getHeight() + 2 * UIConfig.focusBorderWidth;
                focusImage.changeFocusPos(left, top, viewWidth, viewHeight);
                focusImage.setAlpha(1);
                hoverIndex = v.getId();
            }

            return false;
        }
    };

    class PickAdapter extends BaseAdapter {
        private Context mContext;
        private List<AppItemData> dataList;

        public PickAdapter(Context context, List<AppItemData> dataList) {
            this.mContext = context;
            this.dataList = dataList;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
            if (convertView == null) {
                AppItemView layout = ItemViewFactory.createItemView(mContext,
                        dataList.get(position));
                layout.setFocusable(false);
                layout.setOnHoverListener(hoverListener);
                layout.setLayoutParams(new AbsListView.LayoutParams(UIConfig.hotSeatItemWidth,
                        UIConfig.appItemHeight));
                convertView = layout;
            } else {
                APP_ITEM_TYPE type = APP_ITEM_TYPE.valueOf(dataList.get(position).getType());
                if (type == APP_ITEM_TYPE.APP_ENTRY_TYPE) {
                    ((AppEntry) convertView).update((AppEntryData) dataList.get(position));
                } else if (type == APP_ITEM_TYPE.APP_SHORTCUT_TYPE) {
                    ((AppShortcut) convertView).update((AppShortCutData) dataList.get(position));
                }
                // else if(type == APP_ITEM_TYPE.APP_ENTRY_TYPE)
                // {
                // ((AppWidget) convertView).update((AppWidgetData) dataList.get(position));
                // }
                // else if(type == APP_ITEM_TYPE.APP_ENTRY_TYPE)
                // {
                // ((AppFolder) convertView).update((AppFolderData) dataList.get(position));
                // }
            }
            convertView.setOnClickListener(gridClickListener);
            convertView.setId(position);
            return convertView;
        }

        @Override
        public int getItemViewType(int position) {
            // TODO Auto-generated method stub
            return APP_ITEM_TYPE.valueOf(dataList.get(position).getType()).flag;
        }

        @Override
        public int getViewTypeCount() {
            // TODO Auto-generated method stub
            return 4;
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return 0;
        }

        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            if (dataList != null)
                return dataList.size();
            return 0;
        }
    }

    public boolean processKeyDown(int keyCode) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (menuShown) {
                hideMenu();
                return true;
            }
            // if(gridView.getSelectedItemPosition() != 0)
            // focusImage.animate().alpha(0).setDuration(focusImageFadeDuration);
            boundaryDetector.switchToHotSeat();
            GuideViewManager.getInstance().hideMe();
            return true;
        } else if (keyCode == KeyEvent.KEYCODE_MENU) {
            Tip readTip = GuideViewManager.getTipByKeyCode(keyCode, GuideTips.ALLAPPS_TIPS);
            if (readTip != null)
                GuideViewManager.getInstance().readTip(readTip);

            clickMenuPos = gridView.getSelectedItemPosition();
            if (clickMenuPos < 0 || clickMenuPos >= gridView.getAdapter().getCount())
                return false;
            menuData = dataList.get(clickMenuPos);
            showMenu(menuData);
        }

        return false;
    }

    private BoundaryDetector boundaryDetector;

    public void setBoundaryDetector(BoundaryDetector boundaryDetector) {
        this.boundaryDetector = boundaryDetector;
    }

    public void readyToShow() {
//        initDataList();
        while (gridView == null) {
            try {
                Thread.sleep(50);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        if(gridView == null)
            return;
        gridView.scrollTo(0, 0);
        if(gridView.getAdapter() != null && gridView.getAdapter() instanceof BaseAdapter) {
            ((BaseAdapter) gridView.getAdapter()).notifyDataSetChanged();
        }
    }

    public void resetFocus() {
        if (dataList != null && dataList.size() > 0) {
            doFocusAnim = false;
            gridView.setSelection(0);
            gridView.requestFocus();
            gridItemSelectListener.onItemSelected(gridView, gridView.getChildAt(0), 0, 0);
        }

        GuideViewManager.getInstance().showMe(GuideTips.ALLAPPS_TIPS,
                Gravity.CENTER_HORIZONTAL | Gravity.TOP, 0, SkyConstants.Div(40));
    }

    private void showMenu(AppItemData data) {
        if (data == null) {
            TipToast.getInstance(mContext).showTipToast("打开菜单失败");
            return;
        }
        initMenu();
        menu.addOrReplaceAppItemView(data);
        menu.resetFocus();
        menuDialog.show();
        menuShown = true;
        focusImage.animate().alpha(0).setDuration(focusImageFadeDuration);
    }

    private void hideMenu() {
        menuDialog.hide();
        menuShown = false;
        focusImage.animate().alpha(1).setDuration(focusImageFadeDuration);
    }

    private void initMenu() {
        if (menu == null) {
            menuDialog = new Dialog(mContext, R.style.full_transparent_dlg);
            menuDialog.getWindow().setWindowAnimations(R.style.dialogBottomAnim);
            menuDialog.getWindow().setDimAmount(0.75f);
            menuDialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
                @Override
                public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                    // TODO Auto-generated method stub
                    if (event.getAction() == KeyEvent.ACTION_DOWN) {
                        if (keyCode == KeyEvent.KEYCODE_MENU) {
                            hideMenu();
                        } else if (keyCode == KeyEvent.KEYCODE_BACK) {
                            hideMenu();
                        } else if (keyCode == KeyEvent.KEYCODE_HOME
                                && HomeKeymanager.getInstance().isCooCaaVer5()
                                && HomeKeymanager.getInstance().getAppStoreIsHomePage()) {
                            hideMenu();
                            boundaryDetector.switchToHotSeat();
                        }
                    }
                    return false;
                }
            });
            WindowManager.LayoutParams dlgParams = menuDialog.getWindow().getAttributes();
            dlgParams.gravity = Gravity.BOTTOM;
            menuDialog.getWindow().setAttributes(dlgParams);

            menu = new AllAppMenuView(mContext,
                    (Utils.getScreenWidth(mContext) - gridViewWidth) / 2);
            menu.setBtnClickListener(menuBtnClickListener);
            menuDialog.setContentView(menu, new LayoutParams(Utils.getScreenWidth(mContext),
                    AllAppMenuView.menuHeight));
        }
    }

    private OnClickListener menuBtnClickListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            if (v.getId() == AllAppMenuView.ADD_TO_HOT_SEAT_BTN_ID) {
                List<AppItemData> hotSeatList = NormalController.hostContext.getAllItems(false);
                for (AppItemData data : hotSeatList) {

                    try {
                        if (data.getMainActivity().equals(menuData.getMainActivity())) {
                            hideMenu();
                            TipToast.getInstance(mContext).showToast(
                                    SkinManager.getInstance().getDrawable(null,
                                            R.drawable.toast_icon_tip),
                                    SkinManager.getInstance().getString(R.string.app_already_added),
                                    TipToast.defaultDuration, Gravity.CENTER);
                            return;
                        }
                    } catch (Exception e) {
                        // TODO: handle exception
                    }
                }
                if (menuListener != null) {
                    AppItemData addData;
                    boolean addResult = false;
                    try {
                        addData = menuData.clone();
                        addResult = menuListener.onAppAddToHotSeat(addData);
                    } catch (CloneNotSupportedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    if (addResult) {
                        hideMenu();
                        TipToast.getInstance(mContext).showToast(
                                SkinManager.getInstance()
                                        .getDrawable(null, R.drawable.toast_icon_ok),
                                SkinManager.getInstance().getString(R.string.add_succeeded),
                                TipToast.defaultDuration, Gravity.CENTER);
                        return;
                    }
                }
                TipToast.getInstance(mContext).showToast(
                        SkinManager.getInstance().getDrawable(null, R.drawable.toast_icon_tip),
                        SkinManager.getInstance().getString(R.string.hot_seat_full),
                        TipToast.defaultDuration, Gravity.CENTER);
                hideMenu();
            } else if (v.getId() == AllAppMenuView.REMOVE_APP_BTN_ID) {
                if (menuListener != null) {
                    menuListener.onAppUninstall(menuData);
                }
                hideMenu();
            }
        }
    };

    private AllAppMenuListener menuListener;

    public void setMenuListener(AllAppMenuListener listener) {
        this.menuListener = listener;
    }

    public interface AllAppMenuListener {
        public void onAppUninstall(AppItemData data);

        public boolean onAppAddToHotSeat(AppItemData data);
    }

    public void appListChanged() {
        ((Activity) mContext).runOnUiThread(new Runnable() {
            @Override
            public void run() {
                // TODO Auto-generated method stub
                initDataList();
                ((BaseAdapter) gridView.getAdapter()).notifyDataSetChanged();
            }
        });
    }
}
